<?php

    class Roles extends ActiveRecord {
        
        function Roles()
        {
            $conf = array('table'=>DB_PREFIX.'roles');
            $this->ActiveRecord($conf);
        }
        
        function validate()
        {
            $this->validates_presence_of(array('role_name'));
            $this->validates_uniqueness_of('role_name');
        }
        
        function get_user_roles($user_id)
        {
            return $this->db->GetAssoc('SELECT 
                                          u.role_id, r.role_name 
                                        FROM
                                          `'.$this->table.'` r, 
                                          `'.DB_PREFIX.'roles_users` u
                                        WHERE
                                         u.user_id =?
                                        AND
                                         r.id = u.role_id',array($user_id));
        }
        
        function get_user_role_ids($user_id)
        {
            return $this->db->GetCol('SELECT 
                                          u.role_id
                                        FROM
                                          `'.$this->table.'` r, 
                                          `'.DB_PREFIX.'roles_users` u
                                        WHERE
                                         u.user_id =?
                                        AND
                                         r.id = u.role_id',array($user_id));
        }
        
        function has_role($role, $user_id)
        {
            // check db for role
            $has_role = $this->db->GetOne( 'SELECT 
                                              role_id
                                            FROM 
                                              `'.$this->table.'` r, 
                                              `'.DB_PREFIX.'roles_users` ru
                                            WHERE 
                                              r.role_name = ?
                                            AND
                                              ru.role_id = r.id
                                            AND
                                              ru.user_id = ? ',
                                            array($role,$user_id));
            return (bool) $has_role;   
        }
        
        function clear_user_roles($id)
        {
            $this->db->Execute('DELETE FROM `'.DB_PREFIX.'roles_users` WHERE user_id=?',array($id));
        }
        
        function clear_role_users($id)
        {
            $this->db->Execute('DELETE FROM `'.DB_PREFIX.'roles_users` WHERE role_id=?',array($id));
        }
        
        function get_all_roles()
        {
            $roles = new Roles();
            return $roles->find(array('search'=>'all','order' =>'role_name'));
        }
        
        function get_roles_for_select()
        {
            return $this->db->GetAssoc('SELECT id, role_name FROM '.$this->table.' ORDER BY role_name');
        }
        
        function put_user_in_role($role_id,$user_id)
        {
            $this->db->Execute('INSERT INTO `'.DB_PREFIX.'roles_users` (role_id,user_id) VALUES (?,?)',array($role_id,$user_id));
        }
        
        function get_role_users($role_id)
        {
            return $this->db->GetAssoc("SELECT 
                                          u.id, 
                                          CONCAT(u.first_name,' ',u.last_name) 
                                        FROM 
                                          `".DB_PREFIX."roles_users` r, `".DB_PREFIX."users` u
                                        WHERE
                                          r.role_id = ?
                                        AND
                                          r.user_id = u.id",
                                        array($role_id));
        }
        
        function before_delete()
        {
            if ( $this->get('id') == 1 )
            {
                $this->add_error('Role Error','You can not delete the admin role');
            }
            $this->role_to_delete = $this->get('id');
        }
        
        function after_delete()
        {
            $this->clear_role_users($this->role_to_delete);
        }
    }

?>